rm(list=ls())
gc()
library(data.table)
library(modelsummary)
library(stringr)
library(dplyr)
library(xtable)
library(tibble)
library(tidyr)
library(gt)
library(readr)

load('results/sds/sds-2008-2012-main-nohh.Rdata')

n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)
n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)


d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)
d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)
n1_0812=n1
n1/d1
n2_0812=n2
n2/d2

dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Democrats']

reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Republicans']

oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Non-Partisans']

p = rbind(dems,reps,oths)

p1 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

##########

load('results/sds/sds-2012-2016-main-nohh.Rdata')

n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)
n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)


d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)
d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

n1_1216=n1
n1/d1
n2_1216=n2
n2/d2



dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Democrats']

reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Republicans']

oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Non-Partisans']

p = rbind(dems,reps,oths)


p2 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]



####

load('results/sds/sds-2016-2020-main-nohh.Rdata')

n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)
n2 = sum(out[['Democrats - Rep Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)


d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)
d2 = sum(out[['Democrats - Rep Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

n1_1620=n1
n1/d1
n2_1620=n2
n2/d2


dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Democrats']

reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Republicans']

oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Non-Partisans']

p = rbind(dems,reps,oths)

p3 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

####

load("results/sds/sds-2012-2016-nohh-pretrend.Rdata")

n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)
n2 = sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)


d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)
d2 = sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

n1
n1/d1
n2
n2/d2


dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Democrats']

reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Republicans']

oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Non-Partisans']

p = rbind(dems,reps,oths)


p4 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c( 'Exposure Type','Subset')]

##


load('results/sds/sds-2016-2020-nohh-pretrend.Rdata')

n1=sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Dem Exp']][n>1]$n)+sum(out[['Non-partisans - Dem Exp']][n>1]$n)
n2 = sum(out[['Democrats - Dem Exp']][n>1]$n)+sum(out[['Republicans - Rep Exp']][n>1]$n)+sum(out[['Non-partisans - Rep Exp']][n>1]$n)


d1=sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Dem Exp']]$n)+sum(out[['Non-partisans - Dem Exp']]$n)
d2 = sum(out[['Democrats - Dem Exp']]$n)+sum(out[['Republicans - Rep Exp']]$n)+sum(out[['Non-partisans - Rep Exp']]$n)

n1
n1/d1
n2
n2/d2


dems = rbind(out[['Democrats - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Democrats - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Democrats']

reps = rbind(out[['Republicans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Republicans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Republicans']

oths = rbind(out[['Non-partisans - Dem Exp']][,-c('GroupSpDemExp')][,`Exposure Type`:='Democratic'],
       out[['Non-partisans - Rep Exp']][,-c('GroupSpRepExp')][,`Exposure Type`:='Republican']
       
)[,Subset:='Non-Partisans']

p = rbind(dems,reps,oths)

p5 =p[,list(sd=weighted.mean(sd, w= n,na.rm=T)),by=c('Exposure Type','Subset')]

###
p1 = as_tibble(p1)%>%
 mutate(sd=round(sd,3))
p2 = as_tibble(p2)%>%
 mutate(sd=round(sd,3))
p3 = as_tibble(p3)%>%
 mutate(sd=round(sd,3))
p4 = as_tibble(p4)%>%
 mutate(sd=round(sd,3))
p5 = as_tibble(p5)%>%
 mutate(sd=round(sd,3))

p=p1 %>%
 left_join(p2, by=c( 'Exposure Type','Subset'))%>%
 left_join(p3, by=c( 'Exposure Type','Subset'))%>%
 left_join(p4, by=c( 'Exposure Type','Subset'))%>%
 left_join(p5, by=c( 'Exposure Type','Subset'))%>%
 gt()%>%
  fmt_number(columns = 3:7,
             decimals = 3
             )%>%
  cols_label(
    `sd.x` = '2008-2012',
    `sd.y` = '2012-2016',
    `sd.x.x` = '2016-2020',
    `sd.y.y` = '2012-2016',
    `sd` = '2016-2020'
    )%>%
 tab_spanner('Main Specification',3:5)%>%
 tab_spanner('Pre-Trend Specification',6:7)%>%
 as_latex()


write_file(p, 'tables/TabS5.tex')
